/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import edu.gatech.jiggawatt.office.model.Date;
import edu.gatech.jiggawatt.office.model.Doctor;
import edu.gatech.jiggawatt.office.model.Nurse;
import edu.gatech.jiggawatt.office.model.Time;
import edu.gatech.jiggawatt.office.model.TreatmentRecord;
import edu.gatech.jiggawatt.office.presenter.Presenter;
import javax.swing.JTextField;


/**
 * Represents edit treatment record screen
 * @author 40 Jiggawatt Hard Driver *
 * @version 1.0
 */

public class editTreatmentRecordFrame extends JFrame { // $codepro.audit.disable classNamingConvention, declareDefaultConstructors

	private TreatmentRecord currentTr;
	private JPanel contentPane;
	private JTextField dateField;
	private DefaultComboBoxModel doctorModel;
	private DefaultComboBoxModel nurseModel;
	private JComboBox comboBox_treatingDoctor;
	private JComboBox comboBox_attendingNurse;
	private int year, month, day, hour, minute;	
	private Presenter presenter;
	private DefaultListModel trModel;
	private String userId;
	private JTextField timeField;
	private JTextField bloodPressureField;
	private JTextField pulseField;
	private JTextField temperatureField;
	private JTextField heightField;
	private JTextField weightField;
	private JTextArea diagnosisArea;
	private JTextArea chiefComplaintArea;

	/**
	 * Create the frame.
	 * @param p presenter
	 * @param id id of patient to which this treatment record belongs to
	 * @param tr treatment record to edit
	 * @param m treatmentlist model to update
	 */
	public editTreatmentRecordFrame(Presenter p, String id, TreatmentRecord tr, DefaultListModel m)
	{
		currentTr = tr;
		presenter = p;
		trModel = m;
		userId = id;
			
		setTitle("edit Treatment Record"); // $codepro.audit.disable stringLiterals
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 645, 435); // $codepro.audit.disable numericLiterals
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); // $codepro.audit.disable numericLiterals
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		Calendar calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH) + 1;		
		day = calendar.get(Calendar.DAY_OF_MONTH);
		hour = calendar.get(Calendar.HOUR_OF_DAY);
		minute = calendar.get(Calendar.MINUTE);
		
		doctorModel = new DefaultComboBoxModel();
		for (Doctor e : presenter.getDoctorList())
		{
			doctorModel.addElement(e.getFullName());
		}
		
		nurseModel = new DefaultComboBoxModel();
		for (Nurse e : presenter.getNurseList())
		{
			nurseModel.addElement(e.getFullName());
		}
		
		
		JLabel lblTreatingDoctor = new JLabel("Treating Doctor:"); // $codepro.audit.disable stringLiterals
		lblTreatingDoctor.setBounds(24, 28, 82, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblTreatingDoctor);
		
		JLabel lblDate = new JLabel("Date (YYYY/MM/DD):"); // $codepro.audit.disable stringLiterals
		lblDate.setBounds(24, 63, 116, 22); // $codepro.audit.disable numericLiterals
		contentPane.add(lblDate);
		
		comboBox_treatingDoctor = new JComboBox(doctorModel);
		comboBox_treatingDoctor.setSelectedItem(tr.getTreatingDoctor());
		comboBox_treatingDoctor.setBounds(137, 25, 155, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(comboBox_treatingDoctor);
		
		comboBox_attendingNurse = new JComboBox(nurseModel);
		comboBox_attendingNurse.setSelectedItem(tr.getAttendingNurse());
		comboBox_attendingNurse.setBounds(137, 136, 155, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(comboBox_attendingNurse);
		
		JButton btnBack = new JButton("Back"); // $codepro.audit.disable stringLiterals
		btnBack.setBounds(343, 356, 116, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(btnBack);
		
		JButton btnUpdate = new JButton("Update"); // $codepro.audit.disable stringLiterals
		btnUpdate.setBounds(166, 356, 126, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(btnUpdate);
		
		dateField = new JTextField();
		dateField.setEditable(false);
		dateField.setEnabled(false);
		dateField.setBounds(137, 64, 155, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(dateField);
		dateField.setColumns(10); // $codepro.audit.disable numericLiterals
		dateField.setText(tr.getDate().toString());
		
		timeField = new JTextField();
		timeField.setEnabled(false);
		timeField.setEditable(false);
		timeField.setBounds(137, 99, 155, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(timeField);
		timeField.setColumns(10);
		timeField.setText(tr.getTime().toString());
		
		JLabel lblTime = new JLabel("Time:"); // $codepro.audit.disable stringLiterals
		lblTime.setBounds(24, 102, 107, 14);
		contentPane.add(lblTime);
		
		JLabel lblAttendingNurse = new JLabel("Attending Nurse:"); // $codepro.audit.disable stringLiterals
		lblAttendingNurse.setBounds(24, 139, 96, 14);
		contentPane.add(lblAttendingNurse);
		
		JLabel lblChiefCompaint = new JLabel("Chief Compaint:"); // $codepro.audit.disable stringLiterals
		lblChiefCompaint.setBounds(24, 184, 82, 14);
		contentPane.add(lblChiefCompaint);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(137, 184, 155, 135);
		contentPane.add(scrollPane);
		
		chiefComplaintArea = new JTextArea();
		scrollPane.setViewportView(chiefComplaintArea);
		chiefComplaintArea.setText(tr.getChiefComplaint());
		
		JLabel lblBloodPressure = new JLabel("Blood Pressure:"); // $codepro.audit.disable stringLiterals
		lblBloodPressure.setBounds(334, 28, 108, 14);
		contentPane.add(lblBloodPressure);
		
		bloodPressureField = new JTextField();
		bloodPressureField.setBounds(434, 25, 155, 20);
		contentPane.add(bloodPressureField);
		bloodPressureField.setColumns(10);
		bloodPressureField.setText(tr.getBloodPressure() + "");
		
		JLabel lblPulse = new JLabel("Pulse:"); // $codepro.audit.disable stringLiterals
		lblPulse.setBounds(334, 56, 46, 14);
		contentPane.add(lblPulse);
		
		pulseField = new JTextField();
		pulseField.setBounds(434, 53, 155, 20);
		contentPane.add(pulseField);
		pulseField.setColumns(10);
		pulseField.setText(tr.getBloodPressure() + "");
		
		JLabel lblTemperature = new JLabel("Temperature:"); // $codepro.audit.disable stringLiterals
		lblTemperature.setBounds(334, 87, 82, 14);
		contentPane.add(lblTemperature);
		
		JLabel lblWeight = new JLabel("Weight:"); // $codepro.audit.disable stringLiterals
		lblWeight.setBounds(334, 149, 46, 14);
		contentPane.add(lblWeight);
		
		JLabel lblHeight = new JLabel("Height:"); // $codepro.audit.disable stringLiterals
		lblHeight.setBounds(334, 118, 46, 14);
		contentPane.add(lblHeight);
		
		temperatureField = new JTextField();
		temperatureField.setBounds(434, 84, 155, 20);
		contentPane.add(temperatureField);
		temperatureField.setColumns(10);
		temperatureField.setText(tr.getTemperature() + "");
		
		heightField = new JTextField();
		heightField.setBounds(434, 115, 155, 20);
		contentPane.add(heightField);
		heightField.setColumns(10);
		heightField.setText(tr.getHeight() + "");
		
		weightField = new JTextField();
		weightField.setBounds(434, 146, 155, 20);
		contentPane.add(weightField);
		weightField.setColumns(10);
		weightField.setText(tr.getWeight() + "");
		
		JLabel lblDiagnosis = new JLabel("Diagnosis:"); // $codepro.audit.disable stringLiterals
		lblDiagnosis.setBounds(334, 184, 70, 14);
		contentPane.add(lblDiagnosis);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(434, 184, 155, 135);
		contentPane.add(scrollPane_1);
		
		diagnosisArea = new JTextArea();
		scrollPane_1.setViewportView(diagnosisArea);
		diagnosisArea.setText(tr.getDiagnosis());
		
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
					if (comboBox_treatingDoctor.getSelectedIndex() == -1 || comboBox_attendingNurse.getSelectedIndex() == -1 
							|| "".equals(chiefComplaintArea.getText()) || "".equals(bloodPressureField.getText()) || "".equals(pulseField.getText())
								|| "".equals(temperatureField.getText()) || "".equals(heightField.getText()) || "".equals(weightField.getText())
									|| "".equals(diagnosisArea.getText()))
					{
						JOptionPane.showMessageDialog(null, "You need to fill all items",  // $codepro.audit.disable stringLiterals
								"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
					}
					else
					{
						String td = (String) comboBox_treatingDoctor.getSelectedItem();
						Date d = currentTr.getDate();
						Time t = currentTr.getTime(); // $codepro.audit.disable questionableName
						String an = (String) comboBox_attendingNurse.getSelectedItem();
						String cc = chiefComplaintArea.getText();
						double bp = Double.parseDouble(bloodPressureField.getText());
						double p = Double.parseDouble(pulseField.getText()); // $codepro.audit.disable questionableName
						double tp = Double.parseDouble(temperatureField.getText());
						double ht = Double.parseDouble(heightField.getText());
						double wt = Double.parseDouble(weightField.getText());
						String diag = diagnosisArea.getText(); 
						
						TreatmentRecord newTr = new TreatmentRecord(td,d,t,an,cc,bp,p,tp,ht,wt,diag);
						presenter.removeTreatmentRecord(userId, currentTr);
						presenter.addTreatmentRecord(userId, newTr);
						trModel.clear();
						
						for (TreatmentRecord tr : presenter.getTreatmentRecords(userId))
						{
							trModel.addElement(tr);
						}
						
						JOptionPane.showMessageDialog(null, "Treatment record successfully updated!",  // $codepro.audit.disable stringLiterals
								"Success", JOptionPane.INFORMATION_MESSAGE); // $codepro.audit.disable stringLiterals
						
						setVisible(false);
						dispose();
					}
				}
				catch (NumberFormatException e)
				{						
					JOptionPane.showMessageDialog(null, "You must enter numbers for number field",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
				}
			}
		});
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				setVisible(false);
				dispose();
			}
		});
	}	
}
